<#assign f = "#">
<#assign ff = "$">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${cfg.packageName}.mapper.${entity}Mapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${cfg.packageName}.model.entity.${entity}">
        <#list table.fields as field>
            <#if field.keyFlag><#--生成主键排在第一位-->
        <result column="${field.name}" property="${field.propertyName}" />
            </#if>
        </#list>
        <#list table.commonFields as field><#--生成公共字段 -->
         <result column="${field.name}" property="${field.propertyName}" />
        </#list>
        <#list table.fields as field>
            <#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
            </#if>
        </#list>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        <#list table.commonFields as field>
            ${field.columnName},
        </#list>
        ${table.fieldNames}
    </sql>

    <sql id="pageWhere">
        <where>
            <#list table.fields as column>
            <if test="${column.propertyName}!=null and ${column.propertyName}!=''">
                AND t1.${column.name} = ${f}{${column.propertyName}}
            </if>
            </#list>
        </where>
    </sql>

    <select id="select${entity}ById" resultType="${cfg.packageName}.model.bo.${entity}BO">
        SELECT
        <include refid="Base_Column_List" />
        FROM ${table.name} t1
        WHERE <#list table.fields as field><#if field.keyFlag> t1.${field.name} = ${f}{${field.name}}</#if></#list>
    </select>

    <select id="get${entity}List" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM ${table.name} t1
        <include refid="pageWhere" />
    </select>

    <insert id="insertBatch${entity}">
        INSERT INTO ${table.name} (
        <#list table.fields as field>${field.name} <#if field_has_next>,</#if></#list>
        ) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
        <![CDATA[
        (
        <#list table.fields as field>
        ${f}{item.${field.propertyName}}<#if field_has_next>,</#if>
        </#list>
        )
        ]]>
        </foreach>
    </insert>

    <update id="update${entity}ById">
        UPDATE ${table.name} t1
        <#list table.fields as column>
        <if test="${column.propertyName}!=null and ${column.propertyName}!=''">
            SET t1.${column.name} = ${f}{${column.propertyName}}
        </if>
        </#list>
        WHERE <#list table.fields as field><#if field.keyFlag> t1.${field.name} = ${f}{${field.name}}</#if></#list>
    </update>

    <delete id="delete${entity}ById">
        DELETE FROM ${table.name} t1
        WHERE <#list table.fields as field><#if field.keyFlag> t1.${field.name} = ${f}{${field.name}}</#if></#list>
    </delete>
</mapper>
